/**
 * 
 */
package android.coocoo.views;

import java.util.ArrayList;

import android.content.Intent;
import android.coocoo.common.Const;
import android.coocoo.common.TasksListAdapter;
import android.coocoo.common.Util;
import android.coocoo.common.XmlParser;
import android.coocoo.models.CategoryInfo;
import android.coocoo.models.TaskInfo;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

/**
 * @author ThuyTrang
 *
 */
public class DisplayTasksListByCategoryActivity extends ListActivity{
	
	ArrayList<TaskInfo> tasksList;
	ArrayList<CategoryInfo> categoriesList;
	ArrayList<TaskInfo> _tasksList;
	static int currentCategoryId;
	
	Spinner spCategory;
	
	public void setTasksList(ArrayList<TaskInfo> tasksList) {
		this.tasksList = tasksList;
	}

	public ArrayList<TaskInfo> getTasksList() {
		return tasksList;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_tasks_list_bycategory);
        this.tasksList = getIntent().getParcelableArrayListExtra("tasksList");
        this.categoriesList = getIntent().getParcelableArrayListExtra("categoriesList");
        if (this.categoriesList != null)
        	if (this.categoriesList.size()>0)
        		this.currentCategoryId = this.categoriesList.get(0).getId();
        
        spCategory = (Spinner)findViewById(R.id.spCategory);
		if (categoriesList != null){
			String[] arrSpCategory = new String[categoriesList.size()];
			int i;
			for (i=0; i<categoriesList.size(); i++)
				arrSpCategory[i]=categoriesList.get(i).getTitle();
			ArrayAdapter<CharSequence> adSpCategory = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, arrSpCategory);
			adSpCategory.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spCategory.setAdapter(adSpCategory);
			int position=categoriesList.get(0).getId();
//			for (i=0; i<categoriesList.size(); i++)
//				if (categoriesList.get(i).getId()==task.getCategoryId())
//					position=i;
			spCategory.setSelection(position);
			spCategory.setOnItemSelectedListener(new MyCategoryOnItemSelectedListener());
			
			updateDisplay(position);
		}
    }

	public class MyCategoryOnItemSelectedListener implements OnItemSelectedListener {
	    public void onItemSelected(AdapterView parent, View view, int pos, long id) {
//	      Toast.makeText(parent.getContext(), "The Category is " + parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
//	      task.setCategoryId(categoriesList.get(pos).getId());
	    	currentCategoryId = (int) id;
	    	updateDisplay(id);
	    }

		public void onNothingSelected(AdapterView parent) {
	      // Do nothing.
//	    	task.setCategoryId(categoriesList.get(0).getId());
	    }
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
//		System.out.println("Display task list by date reset!");
		this.tasksList = getIntent().getParcelableArrayListExtra("tasksList");
		this.categoriesList = getIntent().getParcelableArrayListExtra("categoriesList");
//		setListAdapter(new TasksListAdapter(this, tasksList));
		updateDisplay(this.currentCategoryId);
//		updateDisplay(year, month, dayOfMonth);
		super.onResume();
	}
	
	private void updateDisplay(long id) {
		// TODO Auto-generated method stub
		int i;
		_tasksList = new ArrayList<TaskInfo>();
		for (i=0; i<tasksList.size(); i++)
			if (tasksList.get(i).getCategoryId() == id){
				_tasksList.add(tasksList.get(i));
			}
		Util.reArrangeTasksList(_tasksList);
		TasksListAdapter tasksListAdapter = new TasksListAdapter(this, _tasksList);
		tasksListAdapter.setByDate(false);
		setListAdapter(tasksListAdapter);
		registerForContextMenu(getListView());
	}
	
	public void onListItemClick(ListView parent, View v, int position, long id) {
//		selection.setText(items[position]);
		Intent intent = new Intent(DisplayTasksListByCategoryActivity.this, DisplayTaskInfoActivity.class);
		TaskInfo _taskInfo = _tasksList.get(position);
		intent.putExtra("task", _taskInfo);
		intent.putExtra("categoriesList", categoriesList);
		startActivityForResult(intent, Const.Request.EDIT_TASK);
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
	  super.onCreateContextMenu(menu, v, menuInfo);
	  MenuInflater inflater = getMenuInflater();
	  inflater.inflate(R.menu.context_task_info, menu);
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
	    // Handle item selection
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
	    switch (item.getItemId()) {
	    case R.id.edit_task:
	    	System.out.print("Choose edit on context menu");
	    	Intent intent = new Intent(DisplayTasksListByCategoryActivity.this, DisplayTaskInfoActivity.class);
			TaskInfo _taskInfo = _tasksList.get(info.position);
			intent.putExtra("task", _taskInfo);
			intent.putExtra("categoriesList", categoriesList);
			startActivityForResult(intent, Const.Request.EDIT_TASK);
	        return true;
	    case R.id.delete_task:
	    	System.out.print("Choose delete on context menu");
	    	int id = _tasksList.get(info.position).getId();
	    	int i;
	    	for (i=0; i<tasksList.size(); i++)
	    		if (tasksList.get(i).getId() == id){
	    			tasksList.remove(i);
	    			updateDisplay(currentCategoryId);
	    		}
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent){
        super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == Const.Request.EDIT_TASK)
        	if (resultCode == Const.Result.SUCCESS){
        		TaskInfo _taskInfo = intent.getParcelableExtra("returnTask");
        		int i;
        		for (i=0; i<tasksList.size(); i++)
        			if (tasksList.get(i).getId() == _taskInfo.getId()){
        				tasksList.remove(i);
        				tasksList.add(i, _taskInfo);
        			}
        		XmlParser xmlParser = new XmlParser();
        		xmlParser.setTasksList(tasksList);
        	}
	}
}
